package com.longge;

import java.util.Stack;

public class MinLength {

    public int minLength(String s) {

        char[] chars = s.toCharArray();


        Stack<Character> stack = new Stack<>();

        for (int i = chars.length - 1; i >= 0; i--) {
            if (!stack.isEmpty()) {
                Character aChar = stack.peek();
                if (aChar == 'B' && chars[i] == 'A') {
                    stack.pop();
                } else if (aChar == 'D' && chars[i] == 'C') {
                    stack.pop();
                }else {
                    stack.push(chars[i]);
                }
            } else {
                stack.push(chars[i]);
            }

        }
        return stack.size();

    }

}
